/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BayesNetwork;

import java.util.ArrayList;

/**
 *
 * @author Jedrek
 */
public class BayesNetwork {
    
    public static float smoothingCoefficient = 1;

    ArrayList<BayesNode> nodes;
    
    BayesNode pitNode;
    BayesNode wumpusNode;

    int radius = 0;

    public BayesNetwork(int radius){
        this.radius = radius;
        
        nodes = new ArrayList<BayesNode>();

        //creating structure of the base network
        pitNode = new BayesNode("pit");
        nodes.add(pitNode);
        wumpusNode = new BayesNode("wumpus");
        nodes.add(wumpusNode);

        BayesNode node;
        for( int c = -radius; c<radius+1; c++)
            for( int r = -radius; r<radius+1; r++){
                if( c == 0 && r == 0)
                    continue;
                node = new BayesNode("wind"+c+r);
                nodes.add(node);
                pitNode.addChild(node);

                node = new BayesNode("sulfur"+c+r);
                nodes.add(node);
                wumpusNode.addChild(node);
            }
    }

    public int getRadius(){
        return radius;
    }

    public float getTrueProbability(String nodeName, ArrayList<String> event){

        BayesNode queryNode = null;

        for(int i = 0; i<nodes.size(); i++){
            if(nodes.get(i).getName().compareTo(nodeName)==0){
                queryNode = nodes.get(i);
                break;
            }
        }

        if(queryNode == null)
            return -1;

        return queryNode.getTrueProbability(event);
    }
    
    public float getTrueProbability(String nodeName, String event){
        ArrayList<String> arrayEvent = new ArrayList<String>();

        if(event.length()<=0)
            return getTrueProbability(nodeName, arrayEvent);
        
        String[] array = event.split(",");
        for(int i = 0; i<array.length; i++){
            arrayEvent.add(array[i].trim());
        }
        
        return getTrueProbability(nodeName, arrayEvent);
    }
    
    public boolean teach(String nodeName, ArrayList<String> event){
        BayesNode queryNode = null;

        for(int i = 0; i<nodes.size(); i++){
            if(nodes.get(i).getName().compareTo(nodeName)==0){
                queryNode = nodes.get(i);
                break;
            }
        }
        
        if(queryNode == null)
            return false;
        
        return queryNode.teach(event);
    }
    
    public boolean teach(String nodeName, String event){
        ArrayList<String> arrayEvent = new ArrayList<String>();

        if(event.length()<=0)
            return teach(nodeName, arrayEvent);
        
        String[] array = event.split(",");
        for(int i = 0; i<array.length; i++){
            arrayEvent.add(array[i].trim());
        }
        
        return teach(nodeName, arrayEvent);
    }
    
    public void teachAllNodes(ArrayList<String> event){
        for(int i = 0; i<nodes.size(); i++){
            teach(nodes.get(i).getName(), event);
        }
    }
    
    public void teachAllNodes(String event){
        for(int i = 0; i<nodes.size(); i++){
            teach(nodes.get(i).getName(), event);
        }
    }

    BayesNode getNode(String name){
        for( int i = 0; i<nodes.size(); i++ ){
            if(nodes.get(i).getName().compareTo(name)==0)
                return nodes.get(i);
        }

        return null;
    }

    public void setMemorySize(int memorySize){
        for(int i = 0; i<nodes.size(); i++)
            nodes.get(i).memorySize = memorySize;
    }
}
